﻿Option Strict On
Option Explicit On

Imports ICtagControl.VBStrings

Public Class clsDbFactoryInfo

    Private SQL As String
    Private ResDt As DataTable

    Public Structure T_Destination
        Dim szIP As String
        Dim szUsr As String
        Dim szPsw As String
    End Structure

    Private dtType As DataTable

    Public ReadOnly Property TypeTable As DataTable
        Get
            TypeTable = dtType
        End Get
    End Property

    Public ReadOnly TYPE_ID As String = "TYPE_ID"
    Public ReadOnly TYPE_NAME As String = "TYPE_NM"

    Public DstInfo As T_Destination

    '***********************************************************************************
    '*  工場の名前を取得
    '***********************************************************************************
    Public Function GetFactoryName() As String

        GetFactoryName = ""

        Try
            SQL = "SELECT STR_PARAM FROM KAD.T_FACTORY_INFO WHERE PARAM_ID = 1"
            ResDt = DBacs.ExecuteSql(SQL)

            If ResDt.Rows.Count > 0 Then
                GetFactoryName = ResDt.Rows(0).Item(0).ToString()
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
        End Try

    End Function

    '***********************************************************************************
    '*  工場のモードを取得
    '***********************************************************************************
    Public Function GetFactoryMode() As Integer

        GetFactoryMode = 0

        Try
            SQL = "SELECT NUM_PARAM4 FROM KAD.T_FACTORY_INFO WHERE PARAM_ID = 608"
            ResDt = DBacs.ExecuteSql(SQL)

            If ResDt.Rows.Count > 0 Then
                GetFactoryMode = CInt(ResDt.Rows(0).Item(0).ToString)
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
        End Try

    End Function

    '***********************************************************************************
    '*  クライアントのバージョンを取得
    '***********************************************************************************
    Public Function GetClientVersion() As String
        Dim i As Integer

        GetClientVersion = "000.000.000"

        Try
            SQL = "SELECT NUM_PARAM1,NUM_PARAM2,NUM_PARAM3 FROM KAD.T_FACTORY_INFO WHERE PARAM_ID = 99014"
            ResDt = DBacs.ExecuteSql(SQL)

            If ResDt.Rows.Count > 0 Then
                GetClientVersion = ""
                For i = 0 To 2
                    If 0 < i Then
                        GetClientVersion = GetClientVersion & "."
                    End If
                    GetClientVersion = GetClientVersion & Format(Val(ResDt.Rows(0).Item(i).ToString), "000")
                Next i
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
        End Try

    End Function

    '***********************************************************************************
    '*  ファイル保存先を取得
    '***********************************************************************************
    Public Sub GetCmdDestination()

        Dim szDst(1) As String

        Try
            SQL = "SELECT STR_PARAM2, STR_PARAM3, STR_PARAM4 FROM KAD.T_FACTORY_INFO WHERE PARAM_ID = 650"
            ResDt = DBacs.ExecuteSql(SQL)

            If ResDt.Rows.Count > 0 Then
                ReDim szDst(3)
                DstInfo.szIP = ResDt.Rows(0)(0).ToString 'IPアドレス
                DstInfo.szUsr = ResDt.Rows(0)(1).ToString 'ユーザー
                DstInfo.szPsw = ResDt.Rows(0)(2).ToString 'パスワード
            Else
                DstInfo.szIP = String.Empty
                DstInfo.szUsr = String.Empty
                DstInfo.szPsw = String.Empty
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            DstInfo.szIP = "ERROR"
        End Try

    End Sub


    '***********************************************************************************
    '*  装置パラメータファイル保存先を取得
    '***********************************************************************************
    Public Sub GetRcpDestination()

        Dim szDst(1) As String

        Try
            SQL = "SELECT STR_PARAM2, STR_PARAM3, STR_PARAM4 FROM KAD.T_FACTORY_INFO WHERE PARAM_ID = 651"
            ResDt = DBacs.ExecuteSql(SQL)

            If ResDt.Rows.Count > 0 Then
                ReDim szDst(3)
                DstInfo.szIP = ResDt.Rows(0)(0).ToString 'IPアドレス
                DstInfo.szUsr = ResDt.Rows(0)(1).ToString 'ユーザー
                DstInfo.szPsw = ResDt.Rows(0)(2).ToString 'パスワード
            Else
                DstInfo.szIP = String.Empty
                DstInfo.szUsr = String.Empty
                DstInfo.szPsw = String.Empty
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            DstInfo.szIP = "ERROR"
        End Try

    End Sub

    '***********************************************************************************
    '*  ファイル保存先を設定
    '***********************************************************************************
    Public Sub SetCmdDestination(ByVal szIP As String, ByVal szUser As String, ByVal szPsw As String)
        Try
            SQL = "UPDATE KAD.T_FACTORY_INFO SET "
            SQL = SQL & "STR_PARAM2 = ?, STR_PARAM3 = ?, STR_PARAM4 = ? "
            SQL = SQL & "WHERE PARAM_ID = ?"

            DBacs.ExecuteUpdateSql(SQL, -1, szIP, szUser, szPsw, 650)
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
        End Try

    End Sub

    Public Sub GetTypesForRcp()
        Try
            SQL = "SELECT PARAM_ID - 400 AS " & TYPE_ID & ","
            SQL = SQL & "  STR_PARAM2 AS " & TYPE_NAME & " "
            SQL = SQL & "FROM KAD.T_FACTORY_INFO "
            SQL = SQL & "WHERE PARAM_ID >=401 "
            SQL = SQL & "  AND PARAM_ID <=499 "
            SQL = SQL & "  AND NUM_PARAM4 = 1 "
            SQL = SQL & "ORDER BY PARAM_ID"

            dtType = DBacs.ExecuteSql(SQL)
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            dtType = New DataTable
        End Try
    End Sub
    'ADD MIAO 2019/1/2 BEGIN
    Public Function GetFTPip() As String
        Dim rtn As String = ""
        Try
            SQL = "SELECT STR_PARAM4 FROM KAD.T_FACTORY_INFO "　'UPD MIAO 2019/1/7
            SQL = SQL & "WHERE PARAM_ID = 99017 "

            Dim dt As DataTable = DBacs.ExecuteSql(SQL)
            If Not IsNothing(dt) AndAlso dt.Rows.Count > 0 Then
                rtn = dt.Rows(0)(0).ToString
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            Return ""
        End Try
        Return rtn
    End Function
    'ADD MIAO 2019/1/2 END

    Public Function GetIcTagSwVersion(Optional ByVal iType As Integer = 0) As String

        Try
            SQL = "SELECT NUM_PARAM1, NUM_PARAM2, NUM_PARAM3 "
            SQL = SQL & "FROM KAD.T_FACTORY_INFO "
            SQL = SQL & "WHERE PARAM_ID = 99017 "
            If iType > 0 Then
                '>>20180131 兪
                SQL = SQL & String.Format("  AND (STR_PARAM3 = '{0}' ", iType)
                SQL = SQL & String.Format(" OR STR_PARAM3 LIKE '{0},%' ", iType)
                SQL = SQL & String.Format(" OR STR_PARAM3 LIKE '%,{0}' ", iType)
                SQL = SQL & String.Format(" OR STR_PARAM3 LIKE '%,{0},%' ", iType)
                '>>20180131 兪
                SQL = SQL & " OR STR_PARAM3 = '0') "
            End If

            Dim dt As DataTable = DBacs.ExecuteSql(SQL)
            If Not IsNothing(dt) AndAlso dt.Rows.Count > 0 Then
                Dim v1, v2, v3 As Integer
                With dt
                    If IsNumeric(.Rows(0)("NUM_PARAM1")) Then
                        v1 = CInt(.Rows(0)("NUM_PARAM1"))
                    Else
                        v1 = 0
                    End If
                    If IsNumeric(.Rows(0)("NUM_PARAM2")) Then
                        v2 = CInt(.Rows(0)("NUM_PARAM2"))
                    Else
                        v2 = 0
                    End If
                    If IsNumeric(.Rows(0)("NUM_PARAM3")) Then
                        v3 = CInt(.Rows(0)("NUM_PARAM3"))
                    Else
                        v3 = 0
                    End If
                    GetIcTagSwVersion = String.Format("{0}.{1}.{2}", v1, v2, v3)
                    'Return FixCharZero(v1.ToString, 3) & FixCharZero(v2.ToString, 3) & FixCharZero(v3.ToString, 3)
                End With
            Else
                Return "000.000.000"
            End If
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            Return "0.0.0"
        End Try
    End Function

    '>>2071225 兪
    Public Function SaveCurrentVersionInfo(ByVal szEquipID As String, ByVal szVersion As String, ByVal szIP As String) As Integer
        Try
            SQL = ""
            SQL &= "UPDATE kad.s_equip SET "
            SQL &= "   SF_VERSION = ? "
            SQL &= "  , SF_IPADRS  = ? "
            SQL &= "WHERE "
            SQL &= "  sys_equip_id = ( "
            SQL &= "  select sys_equip_id from "
            SQL &= "  kad.t_equip_reg "
            SQL &= "  where equip_id = ? "
            SQL &= ") "

            Dim cnt = DBacs.ExecuteUpdateSql(SQL, -1, szVersion, szIP, szEquipID)
            Return cnt
        Catch ex As Exception
            MsgBox("DB Access Error " & System.Reflection.MethodBase.GetCurrentMethod.Name & ":" & ex.Message & ":Sql=" & SQL)
            Return -10
        End Try
    End Function
    '<<2071225 兪

End Class
